<template>
	<!--初始化————授权页面-->
	<view class="page-content" >
		<view class="hello__title">Hello，新同学</view>
		<view class="hello__describe">开始学习为你量身定制的题库和课程吧</view>
		<image class="form__item__image" src="/static/image/user/hello.png"></image>
		<view
			@click="goTo"
			class="user__footer blue_btn">立即开始</view>
	</view>
</template>
<script>
import { mapActions, mapGetters  } from 'vuex'
import  store  from '@/store'
//接口
import { getInfo,getInstitution } from '@/api/authorization'
import { onGetSubject,setUserCourses, getUserCourses} from "@/api/exam_system";
export default {
	data() {
		return {
			is_share:false,
			noGoTo:true,
			courses_id:'',
		}
	},
	onLoad(option) {
		/*let openid='123124'
		this.setOpenid(openid);*/
		//setStorage('open_id',openid);
		//window.localStorage.setItem('openid',openid)
		if(option.pro_token){
			/*if(option.openid){
				this.setOpenid(option.openid);
			}*/
			var pro_token
			pro_token = option.code
			window.localStorage.setItem('pro_token',pro_token)
			const token = option.pro_token;
			state.state.code = token
			setStorage('token', token)
		}
		// #ifdef H5
		var share_key
		if(process.env.NODE_ENV === 'development'){
			share_key = 'axx';
		}else {
			share_key = 'axx';
		}
		window.localStorage.setItem('share_key',share_key)
		if(window.localStorage.getItem('share_key')!==share_key){
			this.noGoTo=false
			console.log("重新授权")
			console.log('store',store.state.user.token)
			window.localStorage.removeItem('pro_token')
			this.setCourses("")
		}
		this.checkInstitution(share_key);
		//#endif
		console.log('token',store.state.token)
		if(store.state.user.courses && this.noGoTo){//已选过科目用户
		    setTimeout(function (e) {
				uni.navigateTo({
					url: '/pages/homeSkin/home'
				})
			},3000)
		}
		this.getStudentInfo();
	},
	computed: {
        ...mapGetters(['userInfo','token'])
    },
	methods: {
		...mapActions('user',['setUserInfo','setOrgInfo','setCourses','setSubject','setToken','setOpenid']),
		goTo() {
			if (this.is_share) {
				uni.showToast({
					title: '请从机构指定入口进入！',
					duration: 2000,
					icon: 'none'
				});
				return
			}
			let share_key = window.localStorage.getItem('share_key');
			this.checkInstitution(share_key);
			this.getStudentInfo();
			onGetSubject({
				subject_type: 10,
				province_num: 3500,
				city_num: 3502,
			}).then(res=>{
				if(res && res.length>0){
					let formdata =[]
					let ass=0
					formdata = res;
					formdata.forEach(item=>{
						if(item.subject_name==='地理'){
							ass = 1
							//设置专业
							this.setSubject(item.id)
							//设置选择科目
							this.setCourses(item.courses)
						}
					})
					if(ass===0){
						uni.showToast({
							title: "请到后台增加《地理》专业和科目",
							icon:'none',
							duration: 3000
						})
					}else{
						uni.navigateTo({url: "/pages/homeSkin/home"});
					}
				}
			})
		},
		// 获取用户信息获取
		getStudentInfo(){
			getInfo().then(res => {
				this.setUserInfo(res)
			})
		},
		// 获取机构信息
		checkInstitution(share_key){
			getInstitution({share_key:share_key}).then(res=>{
				this.setOrgInfo(res)
				if(res.code===105){
					this.is_share=true
					return
				}else{
					this.getStudentInfo();
				}
			})
		}
	}
}
</script>
<style lang="scss">
	.page-content{
		height: 100%;
		text-align: center;
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 150rpx 30rpx 0;
		.hello__title{
			font-size: 44rpx;
			color: #333;
			margin-bottom: 30rpx;
		}
		.hello__describe{
			font-size: 24rpx;
			color: #666;
			margin-bottom: 184rpx;
		}
		.form__item__image{
			width: 516rpx;
			height: 232rpx;
			margin-bottom: 60rpx;
		}
		.user__footer{
			width: 100%;
		}
	}
</style>
